public class demo {
    class Solution {
        int []dx={0,0,1,-1};
        int []dy={1,-1,0,0};
        int m,n;
        int ret=0;
        int [][]memo;
        public int longestIncreasingPath(int[][] matrix) {
            m=matrix.length;
            n=matrix[0].length;
            memo=new int [m][n];
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    ret=Math.max(ret,dfs(matrix,i,j));
                }
            }
            return ret;
        }
        //不要去传参数  递归进行路径判断
        public int dfs(int[][]matrix,int i,int j){
            if(memo[i][j]!=0){
                return memo[i][j];
            }
            int sum=1;
            for(int k=0;k<4;k++){
                int a=i+dx[k];
                int b=j+dy[k];
                if(a>=0&&a<m&&b>=0&&b<n&&matrix[a][b]>matrix[i][j]){
                    sum=Math.max(sum,dfs(matrix,a,b)+1);
                }
            }
            memo[i][j]=sum;
            return sum;
        }
    }
}
